package com.kadron.itemtype;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.dao.KColorDAO;
import com.kadron.dao.KCommonDAO;
import com.kadron.dao.KItemTypeColorDAO;
import com.kadron.dao.KItemTypeMaterialDAO;
import com.kadron.dao.KItemTypeSizeDAO;
import com.kadron.dao.KMaterialDAO;
import com.kadron.dao.KSizeDAO;
import com.kadron.entity.Color;
import com.kadron.entity.ItemType;
import com.kadron.entity.ItemTypeColor;
import com.kadron.entity.ItemTypeMaterial;
import com.kadron.entity.ItemTypeSize;
import com.kadron.entity.Material;
import com.kadron.entity.Size;
import com.kadron.manager.KMessageManager;

@AutoCreate
@Name("kItemTypeManager")
@Scope(ScopeType.PAGE)
public class KItemTypeManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -231909127364887303L;

	private static final Log log = LogFactory.getLog(KItemTypeManager.class);
	
	private ItemType itemType; 
	
	private List<Color> acolor = new ArrayList<Color>();
	private List<Color> scolor = new LinkedList<Color>();
	private List<Size> asize = new ArrayList<Size>();
	private List<Size> ssize = new LinkedList<Size>();
	private List<Material> amaterial = new ArrayList<Material>();
	private List<Material> smaterial = new LinkedList<Material>();
	
	@In
	private KColorDAO colorDAO;
	@In
	private KMaterialDAO materialDAO;
	@In
	private KSizeDAO sizeDAO;
	@In
	private KCommonDAO commonDAO;
	@In
	private KItemTypeColorDAO itemTypeColorDAO;
	@In
	private KItemTypeSizeDAO itemTypeSizeDAO;
	@In
	private KItemTypeMaterialDAO itemTypeMaterialDAO;
	
	@Create
	public void init() {
		Long itemTypeId = (Long) Component.getInstance("itemTypeId");
//		String lang = (String) Component.getInstance("klang");
		if (itemTypeId == null) {
			itemTypeId = new Long(0);
		}
		load(itemTypeId);
	}
	
	private void load(long itemTypeId) {
		if (itemTypeId > 0) {
			itemType = (ItemType) commonDAO.find(itemTypeId, ItemType.class);
			
			acolor = colorDAO.getAvailableColors(itemTypeId);
			scolor = colorDAO.getColors(itemTypeId);
			
			asize = sizeDAO.getAvailableSizes(itemTypeId);
			ssize = sizeDAO.getSizes(itemTypeId);
			
			amaterial = materialDAO.getAvailableMaterials(itemTypeId);
			smaterial = materialDAO.getMaterials(itemTypeId);
		} else {
			itemType = new ItemType();
			acolor = commonDAO.getAll(Color.class);
			ssize = commonDAO.getAll(Size.class);
			amaterial = commonDAO.getAll(Material.class);
		}
	}
	
	public String save() {
		if (itemType == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		if (log.isDebugEnabled()) {
			log.debug("Save ItemType itemTypeId="+itemType.getId());
		}
		itemType = (ItemType) commonDAO.update(itemType);
		// COLOR
		for (Color c : scolor) {
			ItemTypeColor ite = itemTypeColorDAO.find(itemType.getId(), c.getId());
			int ordinal = scolor.indexOf(c);
			if (ite == null) {
				ite = new ItemTypeColor(itemType, c, ordinal);
				commonDAO.update(ite);
			} else if (ordinal != ite.getOrdinal()) {
				ite.setOrdinal(ordinal);
				commonDAO.update(ite);				
			}
		}
		List<ItemTypeColor> saved = itemTypeColorDAO.getItemTypeColor(itemType.getId());
		for (ItemTypeColor ite : saved) {
			if (!scolor.contains(ite.getColor())) {
				ite = (ItemTypeColor) itemTypeColorDAO.update(ite);
				try {
					itemTypeColorDAO.delete(ite);					
				} catch (Exception e) {
					log.error("Can not delete ItemTypeColor");
				}
			}
		}
		// SIZE
		for (Size s : ssize) {
			ItemTypeSize ite = itemTypeSizeDAO.find(itemType.getId(), s.getId());
			int ordinal = ssize.indexOf(s);
			if (ite == null) {
				ite = new ItemTypeSize(itemType, s,ordinal);
				itemTypeSizeDAO.update(ite);
			} else if (ordinal != ite.getOrdinal()) {
				ite.setOrdinal(ordinal);
				itemTypeSizeDAO.update(ite);				
			}
		}
		List<ItemTypeSize> savedSize = itemTypeSizeDAO.getItemTypeSize(itemType.getId());
		for (ItemTypeSize ite : savedSize) {
			if (!ssize.contains(ite.getSize())) {
				ite = (ItemTypeSize) itemTypeSizeDAO.update(ite);
				try {
					itemTypeSizeDAO.delete(ite);					
				} catch (Exception e) {
					log.error("Can not delete ItemTypeSize");
				}
			}
		}
		// Maerial
		for (Material m : smaterial) {
			ItemTypeMaterial ite = itemTypeMaterialDAO.find(itemType.getId(), m.getId());
			int ordinal = smaterial.indexOf(m);
			if (ite == null) {
				ite = new ItemTypeMaterial(itemType, m, ordinal);
				itemTypeMaterialDAO.update(ite);
			} else if (ordinal != ite.getOrdinal()) {
				ite.setOrdinal(ordinal);
				itemTypeMaterialDAO.update(ite);				
			}
		}
		List<ItemTypeMaterial> savedMaterial = itemTypeMaterialDAO.getItemTypeMaterial(itemType.getId());
		for (ItemTypeMaterial ite : savedMaterial) {
			if (!smaterial.contains(ite.getMaterial())) {
				ite = (ItemTypeMaterial) itemTypeMaterialDAO.update(ite);
				try {
					itemTypeMaterialDAO.delete(ite);					
				} catch (Exception e) {
					log.error("Can not delete ItemTypeMaterial");
				}
			}
		}
		good();
		return "item_type";
	}

	public ItemType getItemType() {
		return itemType;
	}

	public void setItemType(ItemType itemType) {
		this.itemType = itemType;
	}

	public List<Color> getAcolor() {
		return acolor;
	}

	public void setAcolor(List<Color> acolor) {
		this.acolor = acolor;
	}


	public List<Color> getScolor() {
		return scolor;
	}


	public void setScolor(List<Color> scolor) {
		this.scolor = scolor;
	}


	public List<Size> getAsize() {
		return asize;
	}


	public void setAsize(List<Size> asize) {
		this.asize = asize;
	}


	public List<Size> getSsize() {
		return ssize;
	}


	public void setSsize(List<Size> ssize) {
		this.ssize = ssize;
	}


	public List<Material> getAmaterial() {
		return amaterial;
	}


	public void setAmaterial(List<Material> amaterial) {
		this.amaterial = amaterial;
	}


	public List<Material> getSmaterial() {
		return smaterial;
	}


	public void setSmaterial(List<Material> smaterial) {
		this.smaterial = smaterial;
	}
	
}
